﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReadWriteCsv;
using System.Text.RegularExpressions;

namespace StockAnomalyDetector
{
    class Repetition : Pattern
    {
        List<int> diffNums = new List<int>();//To store the numbers over 1000, and each number in diffNums is unique(The first filtration)
        List<int> frequence = new List<int>();//To store the frequence of each diffnum
        List<int> goodNumbers = new List<int>();//To store the numbers are not very specical(just like 1000 2000 3000...) in diffNums
        List<int> realFrequence = new List<int>();//To store the frequence of each goodnumber
        List<int[]> timeRegis = new List<int[]>();//存所有 diffNumber的起始时间和终止时间 的List
        List<int[]> finalTime = new List<int[]>();//存所有 goodNumber的起始时间和终止时间 的List
        int now = 0;//指示当前diffNums的尾端位置

        public static List<string> exceptionList = new List<string>() { "stockCode,date,startTime,endTime,quantity,frequency" };//暂时存储所有Csv文件里的Repetition异常
        public static CsvFileWriter writer = new CsvFileWriter(@"D:\股票数据\" + "repetition" + ".csv");

        public Repetition(CsvRow r) : base(r)
        {
        }
        // 加载一个csv文件的数据
        public override void Analyze()
        {
            ParseRow();//使得row[i]( i=1,2,3,4 )被Convert成员函数转化为 time price BorS quantity 

            //Read the file and get diffNums and frequence
            try
            {
                if (quantity > 1000)
                {
                    if (!diffNums.Contains(quantity))
                    {
                        int[] timeAndEnd = new int[2]; //存单个goodNumber的起始时间和终止时间(fuck,就是因为这个放在了while循环的外面，搞了两个多小时）
                        timeAndEnd[0] = time;//该diffNum的初始时间
                        timeAndEnd[1] = 0;
                        timeRegis.Add(timeAndEnd);
                        frequence.Add(0);
                        diffNums.Add(quantity);
                        frequence[now++]++;
                    }//如果读取的新的成交量不存在于diffNum中 就把frequence长度加一 把新的成交量数值加进diffNum 并且更新它对应的frequence和初始时间
                    else
                    {
                        frequence[diffNums.IndexOf(quantity)]++;
                        timeRegis[diffNums.IndexOf(quantity)][1] = time;
                    }//如果存在 更新frequence并且记录有可能是该diffNum出现最后一次的时间
                }

            }
            catch (FormatException e)
            {
                Console.WriteLine(e.ToString());
            }

        }//End the Repetition()

        //读取完一个文件内数据后的处理工作
        public override void End(string sourceName)
        {
            //filter the number too special过滤掉过于特殊的元素
            for (int judge = 1100; judge <= 50000; judge += 100)
            {
                for (int i = 0; i < diffNums.Count; i++)
                {
                    if (diffNums[i] == judge)
                    {
                        frequence[i] = 0;
                    }
                }
            }

            //choose the goodNumber and real frequence
            for (int i = 0; i < frequence.Count; i++)
            {
                if (frequence[i] >= 100)
                {
                    goodNumbers.Add(diffNums[i]);
                    realFrequence.Add(frequence[i]);
                    finalTime.Add(timeRegis[i]);
                }
            }

            //最后的处理工作 把所有Repetition异常数据扔进repetitionList1 待外部UpdateExceptionRecord函数统一操作
            if (goodNumbers.Count != 0)
            {
                for (int i = 0; i < goodNumbers.Count; i++)
                {
                    Record record = new RepetitionRecord
                    {
                        stockcode = Convert.ToString(Regex.Match(sourceName, @"[A-Z]{2}\d{6}")),
                        date = Convert.ToString(Regex.Match(sourceName, @"\d{8}")),
                        turnover = goodNumbers[i],
                        frequency = realFrequence[i],
                        startTime = finalTime[i][0],
                        endTime = finalTime[i][1]

                    };
                    Console.WriteLine("Repetition: "+record);
                    exceptionList.Add(record.ToString());
                }
            }

        }//END function RepetitionEnd()

    }//END class Repetition

    class RepetitionRecord : Record
    {
        public int turnover;
        public int frequency;
        public override string ToString()
        {
            return string.Format("{0},{1},{2}", base.ToString(), turnover, frequency);
        }
    }

}
